package android.FiltrosJava;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

public class EqualizeRGB extends Histogram{
	public BufferedImage applyFilter(BufferedImage src){
		return equalizeHistogram(src);		
	}
	
	private BufferedImage equalizeHistogram(BufferedImage src){			
		int[] hist0 = computeHistogram(src,0);
		int[] hist1 = computeHistogram(src,1);
		int[] hist2 = computeHistogram(src,2);
		hist0 = acumuleNormalizeHistogram(hist0);
		hist1 = acumuleNormalizeHistogram(hist1);
		hist2 = acumuleNormalizeHistogram(hist2);
		
		int width = src.getWidth();
        int height = src.getHeight();    	
    	WritableRaster raster_src = src.getRaster();    	
    	int[] imageData = new int[raster_src.getNumBands()];
    	    	
    	for ( int y = 0; y < height; y++ ) {
    		for ( int x = 0; x < width; x++ ) {
    			raster_src.getPixel(x,y,imageData);
    			
    			imageData[0]= hist0[imageData[0]];
    			imageData[1]= hist1[imageData[1]];
    			imageData[2]= hist2[imageData[2]];
    			
    			raster_src.setPixel(x,y,imageData);
    		}
    	}		
		return src;
	}

}
